//@author  A0100617W

#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace storageTest
{		
	TEST_CLASS(FileTest)
	{

		private:
		


	public:

		//@author  A0100617W
		TEST_METHOD(writefileTest_All_Attribute) // test storage Function with users key in all attributes required
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* timeE =new TimedEvent;
           Event* deadlineE= new DeadlineEvent;
           Event* floatingE = new FloatingEvent;
           DateTime timeEstart,timeEend,deadlineEend;
 
           timeEstart.setMin(0);
           timeEstart.setHour(12);
           timeEstart.setDay(13);
           timeEstart.setMonth(9);
           timeEstart.setYear(2013);

           timeEend.setMin(30);
           timeEend.setHour(12);
           timeEend.setDay(13);
           timeEend.setMonth(9);
           timeEend.setYear(2013);



  
           timeE->setName("timeEvent testing");
           timeE->setImportanceLevel("Low");
           timeE->setLocation("utown");
		   timeE->setStatus("pending");

           timeE->setTimedEventStartTime(timeEstart);
           timeE->setTimedEventEndTime(timeEend);



           timeE->setDisplayID(2);
           timeE->setSearchID(1);
  
           deadlineEend.setMin(45);
           deadlineEend.setHour(16);
           deadlineEend.setDay(15);
           deadlineEend.setMonth(9);
           deadlineEend.setYear(2013);

  
           deadlineE->setName("deadlineEvent testing");
           deadlineE->setImportanceLevel("High");
           deadlineE->setLocation("celc");
		   deadlineE->setStatus("done");
           deadlineE->setDeadlineEventEndtime(deadlineEend);


           deadlineE->setDisplayID(1);
           deadlineE->setSearchID(2);
 

           floatingE->setName("floatingEvent testing");
           floatingE->setImportanceLevel("Normal");
           floatingE->setLocation("utown");
		   floatingE->setStatus("overdue");
           floatingE->setDisplayID(3);
           floatingE->setSearchID(3);

           updatedFile.push_back(timeE);
           updatedFile.push_back(deadlineE);
           updatedFile.push_back(floatingE);

           storage.writeFile(updatedFile);
		   std::vector<std::string> expectedOutput;
		   expectedOutput.push_back("TimedEvent,timeEvent testing,Low,utown,2,1,pending,2013/9/13,12:0,2013/9/13,12:30,");
		   expectedOutput.push_back("DeadlineEvent,deadlineEvent testing,High,celc,1,2,done,2013/9/15,16:45,");
		   expectedOutput.push_back("FloatingEvent,floatingEvent testing,Normal,utown,3,3,overdue,");

		   std::ifstream readFile;
		   readFile.open("storageFile.txt");

		   std::string actualContent;
		   std::vector<std::string> actualOutput;
		   while(std::getline(readFile,actualContent)){
			   actualOutput.push_back(actualContent);
		   }

		   for(int i=0;i<3;i++){
			   Assert::AreEqual(expectedOutput[i],actualOutput[i]);
		   }

		}

		//@author  A0100617W
		TEST_METHOD(writefileTest_Some_Attribute) // test storage write file function with location missiong
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* timeE =new TimedEvent;
           Event* deadlineE= new DeadlineEvent;
           Event* floatingE = new FloatingEvent;
           DateTime timeEstart,timeEend,deadlineEend;
 
           timeEstart.setMin(0);
           timeEstart.setHour(12);
           timeEstart.setDay(13);
           timeEstart.setMonth(9);
           timeEstart.setYear(2013);

           timeEend.setMin(30);
           timeEend.setHour(12);
           timeEend.setDay(13);
           timeEend.setMonth(9);
           timeEend.setYear(2013);



  
           timeE->setName("timeEvent testing");
           timeE->setImportanceLevel("Low");
		   timeE->setStatus("pending");

           timeE->setTimedEventStartTime(timeEstart);
           timeE->setTimedEventEndTime(timeEend);



           timeE->setDisplayID(2);
           timeE->setSearchID(1);
  
           deadlineEend.setMin(45);
           deadlineEend.setHour(16);
           deadlineEend.setDay(15);
           deadlineEend.setMonth(9);
           deadlineEend.setYear(2013);

  
           deadlineE->setName("deadlineEvent testing");
           deadlineE->setImportanceLevel("High");
		   deadlineE->setStatus("done");
           deadlineE->setDeadlineEventEndtime(deadlineEend);


           deadlineE->setDisplayID(1);
           deadlineE->setSearchID(2);
 

           floatingE->setName("floatingEvent testing");
           floatingE->setImportanceLevel("Normal");
		   floatingE->setStatus("overdue");
           floatingE->setDisplayID(3);
           floatingE->setSearchID(3);

           updatedFile.push_back(timeE);
           updatedFile.push_back(deadlineE);
           updatedFile.push_back(floatingE);

           storage.writeFile(updatedFile);
		   std::vector<std::string> expectedOutput;
		   expectedOutput.push_back("TimedEvent,timeEvent testing,Low,,2,1,pending,2013/9/13,12:0,2013/9/13,12:30,");
		   expectedOutput.push_back("DeadlineEvent,deadlineEvent testing,High,,1,2,done,2013/9/15,16:45,");
		   expectedOutput.push_back("FloatingEvent,floatingEvent testing,Normal,,3,3,overdue,");

		   std::ifstream readFile;
		   readFile.open("storageFile.txt");

		   std::string actualContent;
		   std::vector<std::string> actualOutput;
		   while(std::getline(readFile,actualContent)){
			   actualOutput.push_back(actualContent);
		   }

		   for(int i=0;i<3;i++){
			   Assert::AreEqual(expectedOutput[i],actualOutput[i]);
		   }

		}

		//@author  A0100617W
		TEST_METHOD(writefileTest_Status_misssing) // test storage Function with status missing
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* timeE =new TimedEvent;
           Event* deadlineE= new DeadlineEvent;
           Event* floatingE = new FloatingEvent;
           DateTime timeEstart,timeEend,deadlineEend;
 
           timeEstart.setMin(0);
           timeEstart.setHour(12);
           timeEstart.setDay(13);
           timeEstart.setMonth(9);
           timeEstart.setYear(2013);

           timeEend.setMin(30);
           timeEend.setHour(12);
           timeEend.setDay(13);
           timeEend.setMonth(9);
           timeEend.setYear(2013);



  
           timeE->setName("timeEvent testing");
           timeE->setImportanceLevel("Low");
           timeE->setLocation("utown");

           timeE->setTimedEventStartTime(timeEstart);
           timeE->setTimedEventEndTime(timeEend);



           timeE->setDisplayID(2);
           timeE->setSearchID(1);
  
           deadlineEend.setMin(45);
           deadlineEend.setHour(16);
           deadlineEend.setDay(15);
           deadlineEend.setMonth(9);
           deadlineEend.setYear(2013);

  
           deadlineE->setName("deadlineEvent testing");
           deadlineE->setImportanceLevel("High");
           deadlineE->setLocation("celc");

           deadlineE->setDeadlineEventEndtime(deadlineEend);


           deadlineE->setDisplayID(1);
           deadlineE->setSearchID(2);
 

           floatingE->setName("floatingEvent testing");
           floatingE->setImportanceLevel("Normal");
           floatingE->setLocation("utown");
           floatingE->setDisplayID(3);
           floatingE->setSearchID(3);

           updatedFile.push_back(timeE);
           updatedFile.push_back(deadlineE);
           updatedFile.push_back(floatingE);

           storage.writeFile(updatedFile);
		   std::vector<std::string> expectedOutput;
		   expectedOutput.push_back("TimedEvent,timeEvent testing,Low,utown,2,1,,2013/9/13,12:0,2013/9/13,12:30,");
		   expectedOutput.push_back("DeadlineEvent,deadlineEvent testing,High,celc,1,2,,2013/9/15,16:45,");
		   expectedOutput.push_back("FloatingEvent,floatingEvent testing,Normal,utown,3,3,,");

		   std::ifstream readFile;
		   readFile.open("storageFile.txt");

		   std::string actualContent;
		   std::vector<std::string> actualOutput;
		   while(std::getline(readFile,actualContent)){
			   actualOutput.push_back(actualContent);
		   }

		   for(int i=0;i<3;i++){
			   Assert::AreEqual(expectedOutput[i],actualOutput[i]);
		   }

		}



		//@author  A0100617W
		TEST_METHOD(readfileTest_TimedEvent)
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* timeE =new TimedEvent;
           
           DateTime timeEstart,timeEend;
 
           timeEstart.setMin(0);
           timeEstart.setHour(12);
           timeEstart.setDay(13);
           timeEstart.setMonth(9);
           timeEstart.setYear(2013);

           timeEend.setMin(30);
           timeEend.setHour(12);
           timeEend.setDay(13);
           timeEend.setMonth(9);
           timeEend.setYear(2013);



  
           timeE->setName("timeEvent testing");
           timeE->setImportanceLevel("Low");
           timeE->setLocation("utown");
           timeE->setStatus("pending");

           timeE->setTimedEventStartTime(timeEstart);
           timeE->setTimedEventEndTime(timeEend);



           timeE->setDisplayID(2);
           timeE->setSearchID(1);
  
           updatedFile.push_back(timeE);
       

           storage.writeFile(updatedFile);

		   std::vector<Event*> actualOutput;
		   actualOutput=storage.readFile();
		   std::string actualOutputName=actualOutput[0]->getName();
           std::string expectedOutputName="timeEvent testing";



		   std::string actualOutputPri=actualOutput[0]->getImportanceLevel();
		   std::string expectedOutputPri="Low";

		   std::string actualOutputStatus=actualOutput[0]->getEventStatus();
		   std::string expectedOutputStatus="pending";

		   Assert::AreEqual(actualOutputName,expectedOutputName);
		   Assert::AreEqual(actualOutputPri,expectedOutputPri);
		   Assert::AreEqual(actualOutputStatus,expectedOutputStatus);
		}


		//@author  A0100617W
		TEST_METHOD(readfileTest_DeadlineEvent)
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* deadlineE =new DeadlineEvent;
           
           DateTime deadlineEend;
 
       

           deadlineEend.setMin(30);
           deadlineEend.setHour(12);
           deadlineEend.setDay(13);
           deadlineEend.setMonth(9);
           deadlineEend.setYear(2013);



  
           deadlineE->setName("deadlineEvent testing");
           deadlineE->setImportanceLevel("Low");
           deadlineE->setLocation("utown");
           deadlineE->setStatus("pending");

           deadlineE->setDeadlineEventEndtime(deadlineEend);



           deadlineE->setDisplayID(2);
           deadlineE->setSearchID(1);
  
           updatedFile.push_back(deadlineE);
       

           storage.writeFile(updatedFile);

		   std::vector<Event*> actualOutput;
		   actualOutput=storage.readFile();
		   std::string actualOutputName=actualOutput[0]->getName();
           std::string expectedOutputName="deadlineEvent testing";



		   std::string actualOutputPri=actualOutput[0]->getImportanceLevel();
		   std::string expectedOutputPri="Low";

		   std::string actualOutputStatus=actualOutput[0]->getEventStatus();
		   std::string expectedOutputStatus="pending";

		   Assert::AreEqual(actualOutputName,expectedOutputName);
		   Assert::AreEqual(actualOutputPri,expectedOutputPri);
		   Assert::AreEqual(actualOutputStatus,expectedOutputStatus);
		}

		//@author  A0100617W
		TEST_METHOD(readfileTest_FloatingEvent)
		{
			Storage storage;
			std::vector<Event*> updatedFile;

           Event* floatingE =new FloatingEvent;
           

  
           floatingE->setName("floatingEvent testing");
           floatingE->setImportanceLevel("Low");
           floatingE->setLocation("utown");
           floatingE->setStatus("pending");




           floatingE->setDisplayID(2);
           floatingE->setSearchID(1);
  
           updatedFile.push_back(floatingE);
       

           storage.writeFile(updatedFile);

		   std::vector<Event*> actualOutput;
		   actualOutput=storage.readFile();
		   std::string actualOutputName=actualOutput[0]->getName();
           std::string expectedOutputName="floatingEvent testing";



		   std::string actualOutputPri=actualOutput[0]->getImportanceLevel();
		   std::string expectedOutputPri="Low";

		   std::string actualOutputStatus=actualOutput[0]->getEventStatus();
		   std::string expectedOutputStatus="pending";

		   Assert::AreEqual(actualOutputName,expectedOutputName);
		   Assert::AreEqual(actualOutputPri,expectedOutputPri);
		   Assert::AreEqual(actualOutputStatus,expectedOutputStatus);
		}


	};
}